package com.example.chz;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class StudentService {
    private List<Student> students = new ArrayList<>();

    public StudentService() {
        // 初始化一些学生数据
        students.add(new Student(1L, "张三", "李四", "zhangsan@example.com", 20));
        students.add(new Student(2L, "王五", "赵六", "wangwu@example.com", 22));
        students.add(new Student(3L, "陈七", "周八", "chenqi@example.com", 24));
        students.add(new Student(4L, "刘九", "钱十", "liujiu@example.com", 26));
        students.add(new Student(5L, "孙十一", "李十二", "sunshier@example.com", 28));
    }


    // 获取所有学生列表
    public List<Student> getAllStudents() {
        return students;
    }

    // 根据学生ID获取学生信息
    public Student getStudentById(Long id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    // 添加新学生
    public Student addStudent(Student student) {
        students.add(student);
        return student;
    }

    // 更新学生信息
    public void updateStudent(Long id, Student updatedStudent) {
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(id)) {
                students.set(i, updatedStudent);
                return;
            }
        }
    }

    // 删除学生
    public void deleteStudent(Long id) {
        students.removeIf(student -> student.getId().equals(id));
    }
}
